package com.aube.interact;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.interact.controller.DataManager;
import com.aube.interact.model.GoodsItem;
import com.aube.interact.model.GoodsModel;
import com.aube.rvi.CommonDataLoader;

/**
 * Created by huyaonan on 16/3/22.
 */
public class CardGoods extends LinearLayout {

    private View mGoodsLayout, mRoot;
    private int margin=0;
    private GoodsItem item;

    private ImageView mLogo;
    private TextView mTitle, mContent, mPrice, mFavor;

    public CardGoods(Context context) {
        this(context, null);
    }
    public CardGoods(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CardGoods(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRoot = LayoutInflater.from(getContext()).inflate(R.layout.aube_plugin_layout_card_goods, this);

        mGoodsLayout = findViewById(R.id.card_goods_layout);

        mLogo = (ImageView) findViewById(R.id.card_goods_logo);
        mTitle = (TextView) findViewById(R.id.card_goods_title);
        mContent = (TextView) findViewById(R.id.card_goods_content);
        mPrice = (TextView) findViewById(R.id.card_goods_price);
        mFavor = (TextView) findViewById(R.id.card_goods_favor);

        margin = getResources().getDimensionPixelOffset(R.dimen.card_margin_lr);
    }

    public void init(final GoodsModel goods, final GoodsHotpotLayout.IAddFavorListener listener, final IAnimToCart mAnim) {
        //for test
//        if (listener != null) {
//            listener.doFavorEvent();
//            return;
//        }

        if(goods == null || goods.getSize() == 0)
            return;

        item = goods.getGoods().get(0);

        boolean hasCollected = DataManager.getInstance(getContext()).hasCollected(item._id);
        if(hasCollected) {
            mFavor.setBackgroundResource(R.drawable.btn_white);
            mFavor.setText(goods.afterDesc);
        } else {
            mFavor.setBackgroundResource(R.drawable.btn_buy_xml);
            mFavor.setText(goods.beforeDesc);
        }

        mFavor.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean hasCollected = DataManager.getInstance(getContext()).hasCollected(item._id);
                if (hasCollected) {
                    DataManager.getInstance(getContext()).delete(item._id);

                    //remove
                    if (listener != null)
                        listener.cancelFavor(item._id);

                    mFavor.setBackgroundResource(R.drawable.btn_buy_xml);
                    mFavor.setText(goods.beforeDesc);
                } else {
                    DataManager.getInstance(getContext()).update(item._id, item);

                    if (listener != null)
                        listener.doFavorEvent(item);

                    if (mAnim != null) {
                        int width = mLogo.getWidth();
                        int height = mLogo.getHeight();

                        int[] locations = new int[2];
                        mLogo.getLocationInWindow(locations);

                        mAnim.anim(locations[0], locations[1], width, height, item.picurl);
                    }

                    mFavor.setBackgroundResource(R.drawable.btn_white);
                    mFavor.setText(goods.afterDesc);
                }
            }
        });

        CommonDataLoader.getInstance(getContext()).startImageLoader(mLogo, item.picurl, 200, 200);
        mTitle.setText(item.tlTitle);
        mContent.setText(item.tlDesc);
        int priceIndex = item.getPriceIndex();
        if(priceIndex <= 0) {
            mPrice.setText(item.price);
        } else {
            SpannableString ss = new SpannableString(item.price);
            ss.setSpan(new RelativeSizeSpan(0.5f), 0, priceIndex, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            mPrice.setText(ss);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = mGoodsLayout.getMeasuredHeight() + margin*2;
        int width = mGoodsLayout.getMeasuredWidth() + margin*4;

        measureChild(getChildAt(0), MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height - margin, MeasureSpec.EXACTLY));

        setMeasuredDimension(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    public interface IAnimToCart {
        public void anim(float x, float y, int width, int height, String url);
    }

}
